//
//  CalendarView.m
//  SCXCalendar
//
//  Created by 桑赐相 on 2017/12/13.
//  Copyright © 2017年 桑赐相. All rights reserved.
//

#import "CalendarView.h"
#import "CalendarHeaderView.h"
#import "CalendarDayView.h"
#import "YXDateHelpObject.h"
@interface CalendarView()<UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) CalendarHeaderView *headerView;
@property (nonatomic,strong) CalendarDayView *topMonthView;
@property (nonatomic,strong) CalendarDayView *currnetMonthView;
@property (nonatomic,strong) CalendarDayView *nextMonthView;
@property (nonatomic,strong) NSDate *topDate;
@property (nonatomic,strong) NSDate *currentDate;
@property (nonatomic,strong) NSDate *nextDate;
@property (nonatomic,strong) YXDateHelpObject *dateManage;
@end
@implementation CalendarView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initDate];
        [self initSubView];
    }
    return self;
}
-(void)initDate{
    self.dateManage = [YXDateHelpObject manager];
    self.currentDate = [NSDate date];
    self.topDate = [self.dateManage getPreviousMonth:self.currentDate];
    self.nextDate = [self.dateManage getNextMonth:self.currentDate];
    [self setValue:self.currentDate forKey:NSStringFromSelector(@selector(selectedDate))];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshDay:) name:@"didSelectDay" object:nil];
}
-(void)initSubView{
    
    
    self.headerView = [[CalendarHeaderView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 80)];
    self.headerView.date = [self.dateManage getStrFromDateFormat:@"yyyy-MM-dd" Date:self.currentDate];
    [self addSubview:self.headerView];
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,81, WIDTH,[self currentMonthHeight:[NSDate date]]+1)];
    self.scrollView.contentSize = CGSizeMake(WIDTH * 3, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.scrollView setContentOffset:CGPointMake(WIDTH, 0)];
    [self addSubview:self.scrollView];
    
    self.topMonthView = [[CalendarDayView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, self.scrollView.frame.size.height) withDate:self.topDate];
    [self.scrollView addSubview:self.topMonthView];
    
    self.currnetMonthView = [[CalendarDayView alloc]initWithFrame:CGRectMake(WIDTH,0, WIDTH, self.scrollView.frame.size.height) withDate:self.currentDate];
    [self.scrollView addSubview:self.currnetMonthView];
    
    self.nextMonthView = [[CalendarDayView alloc]initWithFrame:CGRectMake(WIDTH * 2,0, WIDTH, self.scrollView.frame.size.height) withDate:self.nextDate];
    [self.scrollView addSubview:self.nextMonthView];
}
-(void)refreshDay:(NSNotification *)notification{
    CalendarDayModel *dayModel = notification.userInfo[@"dayModel"];
    self.headerView.date = dayModel.dateString;
    if (dayModel.isNextMonth) {
        [self.scrollView setContentOffset:CGPointMake(WIDTH * 2, 0) animated:YES];
    }else if (dayModel.isPreviousMonth){
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self changeScrolleViewOffset:scrollView.contentOffset.x];
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self changeScrolleViewOffset:scrollView.contentOffset.x];
}
-(void)changeScrolleViewOffset:(CGFloat)offsetX{
    if(offsetX >= WIDTH *2){
        self.topDate = self.currentDate;
        self.currentDate = self.nextDate;
        self.nextDate = [self.dateManage getNextMonth:self.nextDate];
    }else{
        self.nextDate = self.currentDate;
        self.currentDate = self.topDate;
        self.topDate = [self.dateManage getPreviousMonth:self.topDate];
    }
    self.topMonthView.currentDate = self.topDate;
    self.currnetMonthView.currentDate = self.currentDate;
    self.nextMonthView.currentDate = self.nextDate;
    [self.scrollView setContentOffset:CGPointMake(WIDTH, 0)];
    [self setValue:self.currentDate forKey:NSStringFromSelector(@selector(selectedDate))];
}


-(NSInteger)currentMonthHeight:(NSDate *)date{
    CGFloat itemW = self.frame.size.width / 7;
    return [[YXDateHelpObject manager] getRows:date] * itemW;
}















@end
